﻿using System;
using System.Text;
using System.Windows.Forms;
using iSteam.Sdk;
using iSteam.Sdk.Data;
using iSteam.Sdk.Messaging;
using iSteam.Sdk.SmartObjects;

namespace iSteam.Tester
{
    public partial class testerMain : Form
    {
        private Notification _notification;
        private readonly SteamToken _token;

        public testerMain()
        {
            InitializeComponent();
            _token = new SteamToken();
        }

        #region *** Login ***
        
        private void btnConnect_Click(object sender, EventArgs e)
        {
            LoginStatus loginStatus = _token.Authenticate(txtUsername.Text, txtPassword.Text, txtSteamGuardKey.Text ?? "");

            switch (loginStatus)
            {
                case LoginStatus.SteamGuard:
                    statusLabel.Text = "Please login with the Steam Guard code which have been mailed to you.";
                    break;
                case LoginStatus.LoginSuccessful:
                    statusLabel.Text = "Login Successfull";
                    _notification = new Notification(_token);
                    break;
                case LoginStatus.LoginFailed:
                    statusLabel.Text = "Login Failed";
                    break;
                default:
                    statusLabel.Text = "Wierd Failure";
                    break;
            }
        }

        #endregion

        private void tMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            var parent = (TabControl) sender;

            if (parent.SelectedTab.Text == "Notifications")
            {
                lFriends.DisplayMember = "Nickname";
                lFriends.ValueMember = "SteamId";

                var core = _token.CreateCore();
                var friends = core.GetUserInfo(core.GetFriends());
                lFriends.DataSource = friends;

                _notification.MessageReceived += _notification_MessageReceived;
                _notification.SignUp();
            }
        }

        void _notification_MessageReceived(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            var args =
                (NotificationCompletedEventArgs) e;
            if (args.Messages == null) return;
            var core = _token.CreateCore();

            foreach (var message in args.Messages)
            {
                switch (message.MessageType)
                {
                    case UpdateType.TypingNotification:
                        {
                            Update message1 = message;
                            lNotifications.Invoke(
                                (MethodInvoker)
                                (() =>
                                 lNotifications.Items.Add(DateTime.Now + " - " + core.GetUserInfo(message1.Origin).Nickname +
                                                          " is busy typing a message!")));
                            break;
                        }
                    case UpdateType.Message:
                        {
                            Update message1 = message;
                            lNotifications.Invoke(
                                (MethodInvoker)
                                (() =>
                                 lNotifications.Items.Add(DateTime.Now + " - " + core.GetUserInfo(message1.Origin).Nickname + ": " +
                                                          message1.MessageText)));
                            break;
                        }
                }
            }
        }

        private void btnClear_Click_1(object sender, EventArgs e)
        {
            lNotifications.ClearSelected();
        }

        private void btnMessage_Click(object sender, EventArgs e)
        {
            _notify = true;

            var core = _token.CreateCore();
            core.SendMessage(core.GetUserInfo(lFriends.SelectedValue.ToString()), txtMessage.Text);
        }

        private void lFriends_SelectedIndexChanged(object sender, EventArgs e)
        {
            _notify = true;
        }

        private bool _notify = true;
        private void txtMessage_KeyUp(object sender, KeyEventArgs e)
        {
            if (_notify)
            {
                _notify = false;
                var core = _token.CreateCore();
                core.SendTypingNotification(core.GetUserInfo(lFriends.SelectedValue.ToString()));
            }
        }
    }
}
